<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='标签管理',active='tag'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
  <meta th:name="_csrf" th:content="${_csrf.token}"/>
  <!-- 默认的header name是X-CSRF-TOKEN -->
  <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
</head>
<body class="fixed-left">
<div id="wrapper">
  <div th:replace="back/header::header-body"></div>
  <div class="content-page">
    <div class="content">
      <div class="container">
        <div class="row">
          <div class="col-sm-12">
            <h4 class="page-title">标签管理</h4>
          </div>
          <div class="col-md-12">
            <table class="table table-striped table-bordered">
              <thead>
              <tr>
                <th width="35%">文章标题</th>
                <th width="15%">发布时间</th>
                <th>标签</th>
                <th>操作</th>
              </tr>
              </thead>
              <tbody>
              <th:block th:each="tag : ${tag.list}">
                <tr th:id="${tag.id}">
                  <td>
                    <a th:href="@{'/tag/'+${tag.id}}" th:text="${tag.title}" target="_blank"></a>
                  </td>
                  <td><th:block th:text="${commons.dateFormat(tag.created)}"/></td>
                  <td><th:block th:text="${tag.tags}"/></td>
                  <td>
                    <a th:href="@{'/admin/article/'+${tag.id}}"
                       class="btn btn-primary btn-sm waves-effect waves-light m-b-5">
                      <i class="fa fa-edit"></i> <span>编辑标签</span></a>
                    <a href="javascript:void(0)" th:onclick="'delTag('+${tag.id}+');'"
                       class="btn btn-danger btn-sm waves-effect waves-light m-b-5">
                      <i class="fa fa-trash-o"></i> <span>删除标签</span></a>
                  </td>
                </tr>
              </th:block>

              </tbody>
            </table>
            <div th:replace="comm/paging :: pageAdminNav(${tag})"></div>
          </div>
        </div>
        <div th:replace="back/footer :: footer-content"></div>
      </div>
    </div>
  </div>
</div>
<div th:replace="back/footer :: footer"></div>
<script type="text/javascript">
  /*<![CDATA[*/
  function delTag(id) {
    // 获取<meta>标签中封装的_csrf信息
    var token = $("meta[name='_csrf']").attr("content");
    var header = $("meta[name='_csrf_header']").attr("content");
    if(confirm('确定删除该标签吗?')){
      $.ajax({
        type:'post',
        url : '/admin/tag/delete',
        data: {id:id},
        dataType: 'json',
        beforeSend : function(xhr) {
          xhr.setRequestHeader(header, token);
        },
        success: function (result) {
          if (result && result.success) {
            window.alert("标签删除成功");
            window.location.reload();
          } else {
            window.alert(result.msg || '标签删除失败')
          }
        }
      });
    }
  }
  /*]]>*/
</script>
</body>
</html>